//
//  HomeController.m
//  WidgetDemo
//
//  Created by 航汇聚科技 on 2018/3/12.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "HomeController.h"
#import "ScrollTitleView.h"
#import "CustomCollectionViewFlowLayout.h"
#import <AFNetworking/AFNetworking.h>

@interface HomeController ()<UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, ScrollTitleViewDelegate>
{
    NSDictionary *_dict;
}
@property (weak, nonatomic) IBOutlet UIView *titleContentView;
@property (weak, nonatomic) IBOutlet UICollectionView *contentCollectionView;
@property (nonatomic, strong) ScrollTitleView *scrollTitleView;
@property (weak, nonatomic) IBOutlet UICollectionView *homeCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@end

@implementation HomeController

+ (instancetype)sbForHomeController {
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:NSStringFromClass([self class])];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray *arr = @[@"桃花源记",@"娱乐头条",@"科技前沿",@"社会",@"资讯",@"男神",@"小仙女",@"综艺",@"国外",@"军事",@"要闻",@"重庆本地"];
    UIFont *fnt = [UIFont fontWithName:@"Courier New" size:18.0f];
    UIFont *touchedFnt = [UIFont fontWithName:@"Courier New" size:20.0f];
    
    self.scrollTitleView = [ScrollTitleView xib];
    
    [_scrollTitleView drawWithTitles:arr titleFont:fnt touchedTitleFont:touchedFnt normalTitleColor:[UIColor lightGrayColor] touchedTitleColor:[UIColor redColor] eachTitleHorizontalSpace:20];
    
    [self.titleContentView addSubview:_scrollTitleView];
    _scrollTitleView.delegate = self;
    
    [self.homeCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    CustomCollectionViewFlowLayout *flowlayout = [[CustomCollectionViewFlowLayout alloc] init];
    flowlayout.columnCount = 3;
    flowlayout.columnMargin = 10;
    flowlayout.rowMargin = 10;
    flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.flowLayout = flowlayout;
    
    
    
    NSLog(@"-----%ld",(long)[AFNetworkReachabilityManager manager].networkReachabilityStatus);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - collection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    switch ([[_dict valueForKey:@"tag"] integerValue]) {
        case 0:
            cell.backgroundColor = [UIColor purpleColor];
            break;
        case 1:
            cell.backgroundColor = [UIColor orangeColor];
            break;
        case 2:
            cell.backgroundColor = [UIColor yellowColor];
            break;
        case 3:
            cell.backgroundColor = [UIColor blueColor];
            break;
        case 4:
            cell.backgroundColor = [UIColor cyanColor];
            break;
        case 5:
            cell.backgroundColor = [UIColor magentaColor];
            break;
        case 6:
            cell.backgroundColor = [UIColor redColor];
            break;
        default:
            cell.backgroundColor = [UIColor grayColor];
            break;
    }
    
    return cell;
}

#pragma mark - flowlayout
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//
//    int column = 2;
//    CGFloat width = (([UIScreen mainScreen].bounds.size.width - 20) - 10) / column;
//    if (indexPath.item % 2 == 0) {
//        return CGSizeMake(width, width*1.5);
//    }
//    if(indexPath.item % 3 == 0){
//        return CGSizeMake(width, width*1.8);
//    }
//    return CGSizeMake(width, width*1);
//}
//
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

#pragma mark - ScrollTitleViewDelegate
- (void)showWithTitle:(NSString *)title tag:(NSInteger)tag {
    NSDictionary *dict = @{
                           @"title":title,
                           @"tag":@(tag - 1000)
                           };
    _dict = dict;
    [self.homeCollectionView reloadData];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
        //开启证书验证的,没这个  就不验证证书  https不验证证书是不通过的
        securityPolicy.allowInvalidCertificates = YES;
        [securityPolicy setValidatesDomainName:NO];
        manager.securityPolicy = securityPolicy;
    
    [manager POST:@"http://app2.huibo.com/?apiname=job_recommend&debugmode=&degree_id=&device_token=7abe5c776dee6c931b7a99aa5ffb21d148bc49ac1e6725265abd15b110771bb5&exp_jobsort=&exp_salary=&isDebug=true&location_area_code=0300&mobilesys=ios&offset=0&page_pageno=1&page_pagesize=15&start_work=&time=1521102101&token=mk0k76829533&ver=v4.3.2" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"--------%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

@end
